<?php
namespace Admin\Controller;
use Think\Controller;
/*
	二维码推广
*/
class WymController extends InitController {

	/**
	 * 微页面列表
	 */
	/*public function CardQrcodeList(){
		$where = array();
		$status = I('status');
		if($status){
			$where['status'] = $status == 'n' ? 0 : 1;
		}
		$total = M('GeneralizeQrcode')->where($where)->count('id');
		
		$page = new \Think\Page($total,10);
		
		$pager = $page->show();
		$list = M('GeneralizeQrcode')->where($where)->order('orderdisplay desc,id desc')->limit($page->firstRow.','.$page->listRows)->select();
		
		$this->assign(array(
			'list'=>$list,
			'pager'=>$pager,
			
		));
		
		$this->display('CardQrcodeList');
	}*/
	

	/**
	* 名片带参数二维码
	*/
	public function CardGeneralizeAction () {
		
		$id = M('GeneralizeQrcode')->where(1)->getField('id');

		$result = array();
		if($id){
			$result = M('GeneralizeQrcode')->where(array('id'=>$id))->find();
			if(!empty($result['picture'])){
				$result['picture'] = '/'.$result['picture'];
			}
			$custom_info = M('GeneralizeCustom')->where(array('generalize_id'=>$id))->find();
		}
		
		$title   = I('title'); //名片标题
		$picture = I('picture');//名片背景
		$status  = 1;//名片状态    暂时是单模板所以先默认
		$orderdisplay = 0;//名片排序 暂时是单模板所以先默认
		if(!empty($picture)){
			$picture = substr($picture,1);
		}

		$data['title']   = $title; 
		$data['picture'] = $picture;
		$data['status']  = $status;
		$data['orderdisplay'] = $orderdisplay;

		if (IS_AJAX) {
			
			if (empty($title)) {
				$this->error("请填写标题！");
			}
			
			if(empty($id)){
	
				$wymadd = array(
					'title' => $title,
					'createtime' => NOW_TIME,
					'picture' => $picture,
					'status' => $status
				);
				
				$ac = M('GeneralizeQrcode')->add($wymadd);
			}else {
				$ac = M('GeneralizeQrcode')->where(array('id'=>$id))->save($data);
				$ac = $id;
			}

			if(!empty($ac)){
				//保存自定义参数
				$custom_id = $this->GeneralizeCustom($ac);
			}

			if (!empty($ac) || !empty($custom_id) ) {
				$this->success("保存成功",U('CardGeneralizeAction',array('id'=>$ac,'k'=>'6','sk'=>'10')));
			}
			if(empty($ac) && empty($custom_id)){
				$this->error("保存失败");
			}
	
		}
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
				
		$this->assign(array(
			'result'=>$result,
			'custom_info'=>$custom_info,
			'custom_info_json'=>json_encode($custom_info),
			'topmenu'=>$topmenu,
			'sidebarmenu'=>$sidebarmenu
		));
		$this->display('CardGeneralizeAction');
	}

	//自定义名片编辑、添加
	private function GeneralizeCustom ($id) {
		$generalize_custom_id = I('generalize_custom_id');
		$generalize_id = $id;
		$avger_size = I('avger_size');
		$code_size = I('code_size');
		$nickname_font_size = I('nickname_font_size');
		$nickname_font_color = I('nickname_font_color_text','','trim');
		$nickname_font_color = str_replace(' ', '', $nickname_font_color);
		$avger_pos_x = I('avger_pos_x');
		$avger_pos_y = I('avger_pos_y');
		$nickname_pos_x = I('nickname_pos_x');
		$nickname_pos_y = I('nickname_pos_y');
		$code_pos_x = I('code_pos_x');
		$code_pos_y = I('code_pos_y');

		$CustomData = array(
			'avger_size' => $avger_size,
			'nickname_font_size' => $nickname_font_size,
			'nickname_font_color' => $nickname_font_color,
			'avger_pos_x' => $avger_pos_x,
			'avger_pos_y' => $avger_pos_y,
			'nickname_pos_x' => $nickname_pos_x,
			'nickname_pos_y' => $nickname_pos_y,
			'generalize_id' => $generalize_id,
			'code_size' => $code_size,
			'code_pos_x' => $code_pos_x,
			'code_pos_y' => $code_pos_y,
			'createtime' => NOW_TIME,
		);
		if(empty($generalize_custom_id)){
			$custom_id = M('GeneralizeCustom')->add($CustomData);
			return $custom_id;
		}else{
			$num_rows = M('GeneralizeCustom')->where(array('id' => $generalize_custom_id))->save($CustomData);
			return $num_rows;
		}
	}


	/*public function CardDelete(){
      
        $id = I('id');
        if(empty($id)){
            $this->error('服务器肯定是在开小差',U('CardQrcodeList'));
        }
        
        $ac = M('GeneralizeQrcode')->where(array('id'=>$id))->delete();
        if($ac){
	        $this->success("操作成功",U('CardQrcodeList'));
        }else{
	        $this->success("操作失败");

        }
       
    }*/


}